#include "../mqserver/mq_queue.hpp"
#include <gtest/gtest.h>

mymq::MsgQueueManager::ptr mqmp;

class QueueTest : public testing::Environment
{
    public:
        virtual void SetUp() override
        {
            mqmp = std::make_shared<mymq::MsgQueueManager>("./data/meta.db");
        }

        virtual void TearDown() override
        {
            mqmp->clear();
        }
};

TEST(queue_test,insert_test)
{
    std::unordered_map<std::string,std::string> map = {{"k1","v1"}};
    mqmp->declareQueue("Queue1",true,false,false,map);
    mqmp->declareQueue("Queue2",true,false,false,map);
    mqmp->declareQueue("Queue3",true,false,false,map);
    mqmp->declareQueue("Queue4",true,false,false,map);

    ASSERT_EQ(mqmp->size(),4);
}

TEST(queue_test,select_test)
{
    ASSERT_EQ(mqmp->exists("Queue1"), true);
    ASSERT_EQ(mqmp->exists("Queue2"), true);
    ASSERT_EQ(mqmp->exists("Queue3"), false);
    ASSERT_EQ(mqmp->exists("Queue4"), true);
    mymq::MsgQueue::ptr mqp = mqmp->selectQueue("Queue1");
    ASSERT_NE(mqp.get(), nullptr);
    ASSERT_EQ(mqp->name, "Queue1");
    ASSERT_EQ(mqp->durable, true);
    ASSERT_EQ(mqp->exclusive, false);
    ASSERT_EQ(mqp->auto_delete, false);
    ASSERT_EQ(mqp->getArgs(), std::string("k1=v1&"));
}

TEST(queue_test, remove_test) {
    mqmp->deleteQueue("Queue2");
    ASSERT_EQ(mqmp->exists("Queue2"), false);
}

int main(int argc,char* argv[])
{
    testing::InitGoogleTest(&argc,argv);
    testing::AddGlobalTestEnvironment(new QueueTest);
    int ret = RUN_ALL_TESTS();
    (void)ret;
    return 0;
}